//
//  PurchaseViewCell.m
//  TableBarControllerDemo
//
//  Created by 边果互动 on 2021/3/18.
//

#import "PurchaseViewCell.h"
#import "SourceItemCell.h"
#import <SDWebImage/SDWebImage.h>
#import "SourceBean.h"

#define cell_id_sourceItem @"SourceItemCell"

@interface PurchaseViewCell ()
<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation PurchaseViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self.collectionView registerNib:[UINib nibWithNibName:cell_id_sourceItem bundle:nil] forCellWithReuseIdentifier:cell_id_sourceItem];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.sources.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SourceBean *bean = self.sources[indexPath.row];
    
    SourceItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cell_id_sourceItem forIndexPath:indexPath];
    
    cell.layer.cornerRadius = 6;
    [cell.sourceIv sd_setImageWithURL:[NSURL URLWithString:bean.imgUrl]];
    cell.titleTv.text = bean.title;
    
    return cell;
}

//返回每个单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = self.contentLayout.frame.size.width - 64;
    CGFloat height = self.contentLayout.frame.size.height;
    
    return CGSizeMake(width, 120);
}

/**
 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
 flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 //设置每一行的最小间隔（因为是水平滑动，所以一行就是竖着的）
 flowLayout.minimumLineSpacing = 1.0f;
//        flowLayout.minimumInteritemSpacing = 1.0f;
//        flowLayout.sectionInset = UIEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f);
 _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.bottomView.frame.size.height) collectionViewLayout:flowLayout];
 _collectionView.dataSource = self;
 _collectionView.delegate = self;
 _collectionView.backgroundColor = [UIColor whiteColor];
 _collectionView.showsHorizontalScrollIndicator = NO;
 _collectionView.automaticallyAdjustsScrollIndicatorInsets = NO;
 //设置是否分页滑动，（一页一页的滑）
 _collectionView.pagingEnabled = YES;
 [self.bottomView addSubview:_collectionView];
 */
#pragma mark - 懒加载创建CollectionView
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 0);
        flowLayout.minimumLineSpacing = 8;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 120) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.automaticallyAdjustsScrollIndicatorInsets = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.pagingEnabled = YES;
        [self.contentLayout addSubview:_collectionView];
    }
    return _collectionView;
}

@end
